package control;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.board.BoardService;
import model.board.BoardVO;
import model.member.MemberVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class BoardController extends MultiActionController {
	
	private BoardService boardService;	
	
	
	public BoardController(BoardService boardService) {
		super();
		this.boardService = boardService;
	}



	public ModelAndView write(HttpServletRequest request,HttpServletResponse response,BoardVO vo){
		//System.out.println("디비 실행 전 : "+vo);
		ModelAndView mv = new ModelAndView();
		String path = "index.jsp";
		try {
			HttpSession session = request.getSession(false);
			if(session != null){
				MemberVO memberVO = (MemberVO) session.getAttribute("mvo");
				vo.setMemberVO(memberVO);
				boardService.write(vo);
				path = "redirect:board.do?mode=content&no="+vo.getNo();
				
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		//System.out.println("디비 실행 후 : "+vo);
		
		mv.setViewName(path);
		return mv;
	}
	
	public ModelAndView content(HttpServletRequest request,HttpServletResponse response,BoardVO vo){
		
		BoardVO bvo = null;
		try {
			System.out.println(vo);
			bvo = boardService.content(vo.getNo());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(bvo);
		return new ModelAndView("board/content","bvo",bvo);		
	}
}
